$(document).ready(function () {
    // login.html
    $('#login-first-div').css('height', $(window).height())
    $(window).resize(function () {
        $('#login-first-div').css('height', $(window).height())
    })
    // ----------
    $('#navbox').hover(function () {
        $('#navbox .btn-group').prop('class', 'btn-group open')
    }, function () {
        $('#navbox .btn-group').prop('class', 'btn-group')
    })
    $('#navbox').on('mouseover', function () {
        // 修改密码
        $('#public-update').off('click').click(function () {
            $('#myModal-public').modal({
                backdrop: 'static',
                keyboard: false
            })
            $('#myModal-public .modal-title').html('修改密码')
            $('#myModal-public-body').html(` 
            <p>输入新密码：<input type="text" id="update-newpsw"></p>
            <p>重复新密码：<input type="text" id="update-newpsw1"></p>
        `)
            $('#myModal-public .modal-footer').html(`
            <button type="button" class="btn btn-primary" id="update-psw-qr" style="padding: 2px 20px;">确认</button>
            <button type="button" class="btn" data-dismiss="modal" style="padding: 2px 20px;">关闭</button>
        `)
        })

        // 注销
        $('#cancel').off('click').click(function () {
            $.ajax({
                type: 'GET',
                url: '/common/logout',
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {}
            })
            clearCookie('u-college')
            clearCookie('u-grade')
            clearCookie('u-sno')
            clearCookie('u-major')
            clearCookie('u-clazz')
            clearCookie('u-name')
            clearCookie('login-usertype')
            window.location.href = "login.html"
        })

        // 申请答辩
        $('#askForDefence').off('click').click(function(){
            $('#myModal-public').modal({
                backdrop: 'static',
                keyboard: false
            })
            $('#myModal-public .modal-title').html('申请答辩')
            $('#myModal-public-body').html(` 
            <p>确认要申请答辩吗？</p>
        `)
            $('#myModal-public .modal-footer').html(`
            <button type="button" class="btn btn-primary" id="askForDefence-qr" style="padding: 2px 20px;">确认</button>
            <button type="button" class="btn" data-dismiss="modal" style="padding: 2px 20px;">关闭</button>
        `)
        })
    })
        $('#myModal-public').on('mouseover', function () {
            // 修改密码确认按钮
            $('#update-psw-qr').off('click').click(function () {
                if ($('#update-newpsw').val() == $('#update-newpsw1').val() && $('#update-newpsw').val().length >= 6 && $('#update-newpsw').val().length <= 16) {
                    $('#updatetishi').html('')
                    $.ajax({
                        type: 'POST',
                        url: '/common/changepassword',
                        data: {
                            "password": $('#update-newpsw').val(),
                        },
                        xhrFields: {
                            withCredentials: true
                        },      
                        success: function (data) {
                            console.log(data)
                            if (data.status == 0) {
                                $('#myModal-public-body').html(` 
                                            <p>修改密码成功！</p>
                                        `)
                                $('#myModal-public .modal-footer').html(`
                                            <button type="button" class="btn" data-dismiss="modal" style="padding: 2px 20px;">关闭</button>
                                        `)
                            } else {
                                $('#myModal-public-body').html(data.message)
                                $('#myModal-public .modal-footer').html(`
                                            <button type="button" class="btn" data-dismiss="modal" style="padding: 2px 20px;">关闭</button>
                                        `)
                            }
                        }                 
                    })
                } else if ($('#update-newpsw').val() == "") {
                    $('#update-newpsw1').val('')
                    $('#updatetishi').html('')
                    $('#update-newpsw1').after('<p id="updatetishi" style="color:red;margin-top:10px;margin-bottom:-16px">密码不能为空！</p>')
                } else if ($('#update-newpsw').val().length < 6 || $('#update-newpsw').val().length > 16) {
                    $('#update-newpsw1').val('')
                    $('#updatetishi').html('')
                    $('#update-newpsw1').after('<p id="updatetishi" style="color:red;margin-top:10px;margin-bottom:-16px">密码长度为6-16位！</p>')
                } else {
                    $('#update-newpsw1').val('')
                    $('#updatetishi').html('')
                    $('#update-newpsw1').after('<p id="updatetishi" style="color:red;margin-top:10px;margin-bottom:-16px">密码不相同，请重新输入！</p>')
                }
            })

            // 申请答辩确认按钮
            $('#askForDefence-qr').off('click').click(function () {
                $.ajax({
                    type: 'GET',
                    url: '/student/askForDefence',   
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (data) {
                        console.log(data)
                        if (data.status == 0) {
                            $('#myModal-public-body').html(` 
                                        <p>申请答辩成功！</p>
                                    `)
                            $('#myModal-public .modal-footer').html(`
                                        <button type="button" class="btn" data-dismiss="modal" style="padding: 2px 20px;">关闭</button>
                                    `)
                        } else {
                            $('#myModal-public-body').html(data.message)
                            $('#myModal-public .modal-footer').html(`
                                        <button type="button" class="btn" data-dismiss="modal" style="padding: 2px 20px;">关闭</button>
                                    `)
                        }
                    }
                })
            })
        })
   
})
//设置cookie
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}
//获取cookie
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return "";
}
//清除cookie  
function clearCookie(name) {
    setCookie(name, "", -1);
}